/*
 * Casteljau.cpp
 *
 *  Created on: 23/04/2013
 *      Author: gonzalo
 */

#include "Casteljau.h"
#include <iostream>
Casteljau::Casteljau() {

}

Casteljau::~Casteljau() {

}

glm::vec3 Casteljau::get_point(GLfloat u) {

	uint final;
	uint indice = 0;
	glm::vec3 punto;
	uint cant_points = control_points.size() / 3;
	//calculation_points.clear();
	//calculation_points.assign(control_points.size() - 1, 0);
	GLfloat x;
	GLfloat y;
	GLfloat z;

	for (uint i = 0; i < (cant_points - 1); i++) {
		x = (1 - u) * control_points[3 * i] + u * control_points[3 * i + 3];
		y = (1 - u) * control_points[3 * i + 1] + u * control_points[3 * i + 4];
		z = (1 - u) * control_points[3 * i + 2] + u * control_points[3 * i + 5];
		calculation_points[3 * i] = x;
		calculation_points[3 * i + 1] = y;
		calculation_points[3 * i + 2] = z;
	}

	final = 2;
	for (uint j = 0; j < cant_points - 2; j++) {
		for (uint i = 0; i < (cant_points - final); i++) {
			x = (1 - u) * calculation_points[3 * i] + u * calculation_points[3 * i + 3];
			y = (1 - u) * calculation_points[3 * i + 1] + u * calculation_points[3 * i + 4];
			z = (1 - u) * calculation_points[3 * i + 2] + u * calculation_points[3 * i + 5];
			calculation_points[3 * i] = x;
			calculation_points[3 * i + 1] = y;
			calculation_points[3 * i + 2] = z;
			//calculation_points[i] = u * calculation_points[i] + (1 - u) * calculation_points[i + 3];
		}
		final++;
	}

	punto.x = calculation_points[0];
	punto.y = calculation_points[1];
	punto.z = calculation_points[2];

	//std::cout << "Punto: (" << punto.x << ";" << punto.y << ";" << punto.z << ")" << std::endl;
	return punto;
}
